package com.webflux.sample.webfluxapp.controller;

import com.webflux.sample.webfluxapp.dto.MessageDTO;
import com.webflux.sample.webfluxapp.remote.MessageFeign;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/info")
@RequiredArgsConstructor
class MessageController {

  private final MessageFeign messageFeign;

  /**
   * http://localhost:8081/info/test?name=1
   */
  @GetMapping("/test")
  public Mono<String> index(@RequestParam("name") String name){
    return messageFeign.testInfo(name);
  }

  /**
   * http://localhost:8080/info
   */
  @GetMapping("/{id}")
  public Mono<MessageDTO> getById(@PathVariable Long id){
    return messageFeign.getById(id);
  }

  /**
   * http://localhost:8080/info/list-all
   */
  @GetMapping("/list-all")
  public Mono<List<MessageDTO>> listAll(){
    return messageFeign.listAll();
  }

  /**
   * http://localhost:8080/info/save
   */
  @PostMapping("/save")
  public Mono<MessageDTO> save(@RequestBody MessageDTO message) {
    return messageFeign.save(message);
  }

}